package ee.videorentalstore.protocols.commands.filmmanagercommands;

import java.util.List;
import ee.videorentalstore.inventories.Film;
import ee.videorentalstore.inventories.FilmInventory;
import ee.videorentalstore.inventories.FilmType;
import ee.videorentalstore.protocols.commands.Command;

public class AddAFilmCommand extends Command
{
	private final int FILM_NAME_POSITION_IN_SPLITTED_INSTRUCTIONS_LIST = 1;
	private final int FILM_TYPE_POSITION_IN_SPLITTED_INSTRUCTIONS_LIST = 2;
	private final String EMPTY_STRING = "";
	
	
	@Override
	public String getCommandDescription() 
	{
		return "ADD A FILM";
	}
	
	
	@Override
	public void executeCommand(String inputStreamInstructionsToProcess) 
	{
		List<String> splittedInstructionsList = 
				getSplittedInputStreamInstructionsToProcess(inputStreamInstructionsToProcess);
		String filmName = getSpecificStringValueOutOfSplittedInstructionsList(
				splittedInstructionsList, FILM_NAME_POSITION_IN_SPLITTED_INSTRUCTIONS_LIST);
		if (filmName != null && getFilmTypeFromSplittedInstructions(splittedInstructionsList) != null)
		{
			Film film = new Film(filmName, 
					getFilmTypeFromSplittedInstructions(splittedInstructionsList));
			FilmInventory.getInstance().addNewFilm(film);
			System.out.println("The film has been successfully added to the inventory!");
		}
		else
		{
			System.out.println("Command could not be executed due to inserted instructions errors!");
		}
	}
	
	
	private FilmType getFilmTypeFromSplittedInstructions(List<String> splittedInstructionsList)
	{
		String filmTypeInString = null;
		try 
		{
			filmTypeInString = splittedInstructionsList.get(
					FILM_TYPE_POSITION_IN_SPLITTED_INSTRUCTIONS_LIST).trim();
		} catch (Exception e) {}
		
		if (filmTypeInString != null && !filmTypeInString.equals(EMPTY_STRING))
		{
			if (filmTypeInString.trim().equalsIgnoreCase("NEW RELEASE"))
			{
				return FilmType.NEW_RELEASE;
			}
			else if (filmTypeInString.trim().equalsIgnoreCase("REGULAR FILM"))
			{
				return FilmType.REGULAR_FILM;
			}		
			else if (filmTypeInString.trim().equalsIgnoreCase("OLD FILM"))
			{
				return FilmType.OLD_FILM;
			}
			else
			{
				return null;
			}
		}
		else
		{
			return null;
		}
	}
	
}
